
window.onload = function () {
    let searchInput=document.getElementById("searchInput")
    let show=document.getElementById("show")
    let userName=document.getElementById("userName").innerText
    let Uname=document.getElementById("Uname").innerText
    let payBill=document.getElementById("payBill")
    let NoPayBill=document.getElementById("NoPayBill")
    let xhr = new XMLHttpRequest();
    xhr.open("get","http://localhost:8080/showAllMenu?userName="+userName,true)
    xhr.send()
    xhr.onload=function () {
        let v= JSON.parse(xhr.responseText)
        searchInput.innerHTML = ''
        show.innerHTML=''
        searchInput.innerHTML+="<div><input id=\"name\" placeholder=\"按菜品名搜索\">\n" +
            "    <input id=\"price\" placeholder=\"按价格搜索\">\n" +
            "    类型：<select id=\"type\" >\n" +
            "        <option></option>\n" +
            "        <option>早餐</option>\n" +
            "        <option>中餐</option>\n" +
            "        <option>午餐</option>\n" +
            "        <option>甜食</option>\n" +
            "        <option>米</option>\n" +
            "        <option>面</option>\n" +
            "    </select>" +
            "<button onclick='search1()'>搜索</button>\n"+
            "</div>"
        show.innerHTML+='<tr  style="border: solid;">\n' +
            '        <th style="border: solid;">菜品名</th>\n' +
            '        <th style="border: solid;">种类</th>\n' +
            '        <th style="border: solid;">价格</th>\n' +
            '      </tr>'
        for (var i = 0; i < v.length; i++) {
            show.innerHTML += "<tr  style=\"border: solid;\">" +
                "<td name='"+ v[i].id +"'  style=\"border: solid;\">" + v[i].name + "</td>" +
                "<td name='"+ v[i].id +"' style=\"border: solid;\">" + v[i].type + "</td>" +
                "<td name='"+ v[i].id +"' style=\"border: solid;\">" + v[i].price + "元</td>" +
                "<td  style=\"border: solid;\">" +" <button id='"+ v[i].id +"' onclick='addBill(this)'>加入订单</button>"+ "</td>" +
                "</tr>"
        }
        show.innerHTML+="<a href='/'>返回</a>"

    }
    let xhr1 = new XMLHttpRequest();
    xhr1.open("get", "http://localhost:8080/showPayBill?shopUserName="+userName+"&userName="+Uname,true)
    xhr1.send()
    xhr1.onload=function (){
        payBill.innerHTML='已支付'
        payBill.innerHTML+="<tr><th style=\"border: solid;\">菜名</th><th style=\"border: solid;\">价格</th style=\"border: solid;\"><th style=\"border: solid;\">类型</th></tr>"
        for (let i=0;i<JSON.parse(xhr1.responseText).length;i++){
            payBill.innerHTML+="<tr><td style=\"border: solid;\">"+JSON.parse(xhr1.responseText)[i].name +"</td><td style=\"border: solid;\">"+JSON.parse(xhr1.responseText)[i].price +"</td><td style=\"border: solid;\">"+ JSON.parse(xhr1.responseText)[i].type+"</td></tr>"
        }
    }
    let xhr2 = new XMLHttpRequest();
    xhr2.open("get", "http://localhost:8080/showNoPayBill?shopUserName="+userName+"&userName="+Uname,true)
    xhr2.send()
    xhr2.onload=function (){
        NoPayBill.innerHTML='未支付'
        NoPayBill.innerHTML+="<tr><th style=\"border: solid;\">菜名</th><th style=\"border: solid;\">价格</th><th style=\"border: solid;\">类型</th></tr>"
        let total=0.0
        for (let i=0;i<JSON.parse(xhr2.responseText).length;i++){
            NoPayBill.innerHTML+="<tr><td style=\"border: solid;\">"+JSON.parse(xhr2.responseText)[i].name +"</td><td style=\"border: solid;\">"+JSON.parse(xhr2.responseText)[i].price +"</td><td style=\"border: solid;\">"+ JSON.parse(xhr2.responseText)[i].type+"</td><td style=\"border: solid;\"><button id='"+ JSON.parse(xhr2.responseText)[i].id+"' onclick='delBill(this)'>取消</button></td></tr>"
            total+=JSON.parse(xhr2.responseText)[i].price
        }
        NoPayBill.innerHTML+="<tr><th>总价：</th><td id='total'>" + total+ "元</td><td><button onclick='pay()'>支付</button></td></tr>"
    }

}

function search1(){
    let show=document.getElementById("show")
    let userName=document.getElementById("userName").innerText
    let name=document.getElementById("name").value
    let p=document.getElementById("price").value
    let type=document.getElementById("type").value
    const xhr = new XMLHttpRequest();
    xhr.open("get", "http://localhost:8080/searchMenu?merchant_name="+userName+"&name="+name+"&p="+p+"&type="+type,true)
    xhr.send()
    xhr.onload = function () {
        show.innerHTML=''
        show.innerHTML+='<tr  style="border: solid;">\n' +
            '        <th style="border: solid;">菜品名</th>\n' +
            '        <th style="border: solid;">种类</th>\n' +
            '        <th style="border: solid;">价格</th>\n' +
            '      </tr>'
        for (var i = 0; i < JSON.parse(xhr.responseText).length; i++) {
            show.innerHTML += "<tr  style=\"border: solid;\">" +
                "<td name='"+ JSON.parse(xhr.responseText)[i].id +"'  style=\"border: solid;\">" + JSON.parse(xhr.responseText)[i].name + "</td>" +
                "<td name='"+ JSON.parse(xhr.responseText)[i].id +"'  style=\"border: solid;\">" + JSON.parse(xhr.responseText)[i].type + "</td>" +
                "<td name='"+ JSON.parse(xhr.responseText)[i].id +"'  style=\"border: solid;\">" + JSON.parse(xhr.responseText)[i].price+ "元</td>" +
                "<td  style=\"border: solid;\">" +" <button id='"+ JSON.parse(xhr.responseText)[i].id +"' onclick='addBill(this)'>加入订单</button>"+ "</td>" +
                "</tr>"
        }
        show.innerHTML+="<a href='/'>返回</a>"


    }
}
function addBill(e){
    let userName=document.getElementById("userName").innerText
    let Uname=document.getElementById("Uname").innerText
    let NoPayBill=document.getElementById("NoPayBill")
    let name=document.getElementsByName(e.id).item(0).innerText
    let price=document.getElementsByName(e.id).item(2).innerText.slice(0,-1)
    let type=document.getElementsByName(e.id).item(1).innerText
    const xhr = new XMLHttpRequest();
    xhr.open("get", "http://localhost:8080/addBill?shopUserName="+userName+"&userName="+Uname+"&name="+name+"&p="+price+"&type="+type,true)
    xhr.send()
    xhr.onload = function (){
    let xhr2 = new XMLHttpRequest();
    xhr2.open("get", "http://localhost:8080/showNoPayBill?shopUserName="+userName+"&userName="+Uname,true)
    xhr2.send()
    xhr2.onload=function (){
        NoPayBill.innerHTML='未支付'
        NoPayBill.innerHTML+="<tr><th style=\"border: solid;\">菜名</th><th style=\"border: solid;\">价格</th><th style=\"border: solid;\">类型</th></tr>"
        let total=0.0
        for (let i=0;i<JSON.parse(xhr2.responseText).length;i++){
            NoPayBill.innerHTML+="<tr><td style=\"border: solid;\">"+JSON.parse(xhr2.responseText)[i].name +"</td><td style=\"border: solid;\">"+JSON.parse(xhr2.responseText)[i].price +"</td><td style=\"border: solid;\">"+ JSON.parse(xhr2.responseText)[i].type+"</td><td style=\"border: solid;\"><button id='"+ JSON.parse(xhr2.responseText)[i].id+"' onclick='delBill(this)'>取消</button></td></tr>"
            total+=JSON.parse(xhr2.responseText)[i].price

        }
        NoPayBill.innerHTML+="<tr><th>总价：</th><td id='total'>" + total+ "元</td><td><button onclick='pay()'>支付</a></td></tr>"
    }
    }
}


function delBill(e){
    let userName=document.getElementById("userName").innerText
    let Uname=document.getElementById("Uname").innerText
    let NoPayBill=document.getElementById("NoPayBill")
    let xhr = new XMLHttpRequest();
    xhr.open("get", "http://localhost:8080/delBill?id="+e.id,false)
    xhr.send()
    let xhr2 = new XMLHttpRequest();
    xhr2.open("get", "http://localhost:8080/showNoPayBill?shopUserName="+userName+"&userName="+Uname,true)
    xhr2.send()
    xhr2.onload=function (){
        NoPayBill.innerHTML='未支付'
        NoPayBill.innerHTML+="<tr><th style=\"border: solid;\">菜名</th><th style=\"border: solid;\">价格</th><th style=\"border: solid;\">类型</th></tr>"
        let total=0.0
        for (let i=0;i<JSON.parse(xhr2.responseText).length;i++){
            NoPayBill.innerHTML+="<tr><td style=\"border: solid;\">"+JSON.parse(xhr2.responseText)[i].name +"</td><td style=\"border: solid;\">"+JSON.parse(xhr2.responseText)[i].price +"</td><td style=\"border: solid;\">"+ JSON.parse(xhr2.responseText)[i].type+"</td><td style=\"border: solid;\"><button id='"+ JSON.parse(xhr2.responseText)[i].id+"' onclick='delBill(this)'>取消</button></td></tr>"
            total+=JSON.parse(xhr2.responseText)[i].price
        }
        NoPayBill.innerHTML+="<tr><th>总价：</th><td id='total'>" + total+ "元</td><td><button onclick='pay()'>支付</button></td></tr>"
    }
}

function pay(){
    let userName=document.getElementById("userName").innerText
    let Uname=document.getElementById("Uname").innerText
    let total=document.getElementById("total").innerText.slice(0,-1)
    let xhr = new XMLHttpRequest();
    xhr.open("get", "http://localhost:8080/pay?shopUserName="+userName+"&userName="+Uname+"&total="+total,true)
    xhr.send()
    xhr.onload = function (){
        alert(xhr.responseText)
        window.location.href="http://localhost:8080/user.html?shopName="+userName
    }
}
let add=document.getElementById("add");
add.onclick=function (){
    let content=document.getElementById("content")
    content.innerHTML=''
    content.innerHTML+="<input id='m' type='number'><button onclick='addM()'>充值</button>"
    content.innerHTML+="<a href='/'>返回</a>"
}
function addM(){
    let Uname=document.getElementById("Uname").innerText
    let m=document.getElementById("m").value
    let xhr = new XMLHttpRequest();
    xhr.open("get", "http://localhost:8080/addM?userName="+Uname+"&total="+m,true)
    xhr.send()
    xhr.onload=function (){
        alert(xhr.responseText)
        window.location.href="http://localhost:8080/"
    }

}


